﻿using System;
using System.IO;
using System.Net;//新加的
using System.Net.Sockets;//新加的
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace RxjhServer
{
    public class SymmetricMethod
    {
        public string Key;
        private SymmetricAlgorithm mobjCryptoService;
        internal string Get_key1()
        {
            string dnString = "wGc0NG4fpqoucUdDSSkpsiDUM3hk9gl3FdteOuLKrVqZe9erk73UuNEANhyPALnhGJvJQZNyAA8EfnwWml59C2WofM14S06MIxEhiEOdBYAcxTxC4Q6XEQXTg1JSTf71acFNjtgIDDBHz8Ga65OCF6Z8igK7VK9PbHH22YixVsTvHWIF8TQqXkrm1LMlnyR4dvYg3D1cWXVbJBJtP8RVZPsR2jeQodSivaaL5b2HYCWGLpbYRmjBa4B6KOf57IUO";
            //            string dnString = @"JGkGOGMGMGMG]GXFnFkG_FkGaGZGtF]GlFWGhGHG`GrF\G^G~GgGkFIGdGaFPFKFVFKGoG_FKF0GzFFGpF7FGGpGbGxF:GfGfGmG{F9F,GyF+G~FFGuGwFFF4F8F1F#FGwG}";
            //string DNString_dnString = World.pWord(dnString, 2);
            //Console.WriteLine(DNString_dnString);
            return dnString;
        }
        internal string Get_key2()
        {
            string dnString = "fxKi5ylOYZXdLDLvBATYbRuD4lQSqPUlxMcN7d43aEYINrNaTolDc6wr6Qh7s4WQy1e29I6hrsqwOWfFUUmrukFWO5jmBio51gvQQw7MtHsLgBgtm9oGw9GCPj2BCNpjJLx5SbP1vvt0ipyIon7uxnIqRO3pE2r8KzyjkHRfDaCe0E1xpC8ZGTZVj45UWh9mMeHPlviKFGEJ2t2c8qAEH8bAkRM9XMBRdJJ4nKkzGuFy3YKHAVSsJmto37Onp1C6";
            //            string dnString = @"JGkGOGMGMGMG]GXFnFkG_FkGaGZGtF]GlFWGhGHG`GrF\G^G~GgGkFIGdGaFPFKFVFKGoG_FKF0GzFFGpF7FGGpGbGxF:GfGfGmG{F9F,GyF+G~FFGuGwFFF4F8F1F#FGwG}";
            //string DNString_dnString = World.pWord(dnString, 2);
            //Console.WriteLine(DNString_dnString);
            return dnString;
        }
        public SymmetricMethod()
        {
            this.mobjCryptoService = new RijndaelManaged();
            this.Key = Get_key1() + Hasher.GetIP();
        }

        public string Decrypto(string Source)
        {
            try
            {
                byte[] buffer1 = Convert.FromBase64String(Source);
                MemoryStream stream1 = new MemoryStream(buffer1, 0, buffer1.Length);
                string sSource = Source;
                int li_len, i, li_asc;
                string ls_i, ls_code = "";
                int li_ret;
                li_len = sSource.Length;
                ls_code = "";
                li_ret = Convert.ToInt32(sSource.ToCharArray(0, 1)[0]) % 10;
                for (i = 2; i < li_len; i = i + 2)
                {
                    li_asc = Convert.ToInt32(sSource.ToCharArray(i, 1)[0]);
                    if (Convert.ToInt32(sSource.ToCharArray(i - 1, 1)[0]) % 2 == 0)
                    {
                        ls_i = ((char)(li_asc + (i - 1) / 2 + li_ret)).ToString();
                    }
                    else
                    {
                        ls_i = ((char)(li_asc - (i - 1) / 2 - li_ret)).ToString();
                    }
                    ls_code = ls_code + ls_i;
                }
                this.mobjCryptoService.Key = this.GetLegalKey();
                this.mobjCryptoService.IV = this.GetLegalIV();
                ICryptoTransform transform1 = this.mobjCryptoService.CreateDecryptor();
                CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Read);
                StreamReader reader1 = new StreamReader(stream2);
                return reader1.ReadToEnd();
            }
            catch
            {
                return "";
            }
        }
        public string Encrypto(string Source)
        {
            byte[] buffer1 = Encoding.UTF8.GetBytes(Source);
            MemoryStream stream1 = new MemoryStream();
            this.mobjCryptoService.Key = this.GetLegalKey();
            this.mobjCryptoService.IV = this.GetLegalIV();
            ICryptoTransform transform1 = this.mobjCryptoService.CreateEncryptor();
            CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
            stream2.Write(buffer1, 0, buffer1.Length);
            stream2.FlushFinalBlock();
            stream1.Close();
            byte[] buffer2 = stream1.ToArray();
            return Convert.ToBase64String(buffer2);
        }

        private byte[] GetLegalIV()
        {
            string text1 = Get_key2();
            this.mobjCryptoService.GenerateIV();
            int num1 = this.mobjCryptoService.IV.Length;
            if (text1.Length > num1)
            {
                text1 = text1.Substring(text1.Length - num1, num1);
            }
            else if (text1.Length < num1)
            {
                text1 = text1.PadRight(num1, ' ');
            }
            return Encoding.ASCII.GetBytes(text1);
        }
        private byte[] GetLegalKey()
        {
            string text1 = this.Key;
            //Console.WriteLine(text1);
            this.mobjCryptoService.GenerateKey();
            int num1 = this.mobjCryptoService.Key.Length;
            if (text1.Length > num1)
            {
                text1 = text1.Substring(text1.Length - num1, num1);
            }
            else if (text1.Length < num1)
            {
                text1 = text1.PadRight(num1, ' ');
            }
            return Encoding.ASCII.GetBytes(text1);
        }
    }

    public class SyjiaetricMethod
    {
        internal string Get_key1()
        {
            string dnString = "wGc0NG4fpqoucUdDSSkpsiDUM3hk9gl3FdteOuLKrVqZe9erk73UuNEANhyPALnhGJvJQZNyAA8EfnwWml59C2WofM14S06MIxEhiEOdBYAcxTxC4Q6XEQXTg1JSTf71acFNjtgIDDBHz8Ga65OCF6Z8igK7VK9PbHH22YixVsTvHWIF8TQqXkrm1LMlnyR4dvYg3D1cWXVbJBJtP8RVZPsR2jeQodSivaaL5b2HYCWGLpbYRmjBa4B6KOf57IUO";
            //            string dnString = @"JGkGOGMGMGMG]GXFnFkG_FkGaGZGtF]GlFWGhGHG`GrF\G^G~GgGkFIGdGaFPFKFVFKGoG_FKF0GzFFGpF7FGGpGbGxF:GfGfGmG{F9F,GyF+G~FFGuGwFFF4F8F1F#FGwG}";
            //string DNString_dnString = World.pWord(dnString, 2);
            //Console.WriteLine(DNString_dnString);
            return dnString;
        }
        internal string Get_key2()
        {
            string dnString = "fxKi5ylOYZXdLDLvBATYbRuD4lQSqPUlxMcN7d43aEYINrNaTolDc6wr6Qh7s4WQy1e29I6hrsqwOWfFUUmrukFWO5jmBio51gvQQw7MtHsLgBgtm9oGw9GCPj2BCNpjJLx5SbP1vvt0ipyIon7uxnIqRO3pE2r8KzyjkHRfDaCe0E1xpC8ZGTZVj45UWh9mMeHPlviKFGEJ2t2c8qAEH8bAkRM9XMBRdJJ4nKkzGuFy3YKHAVSsJmto37Onp1C6";
            //            string dnString = @"JGkGOGMGMGMG]GXFnFkG_FkGaGZGtF]GlFWGhGHG`GrF\G^G~GgGkFIGdGaFPFKFVFKGoG_FKF0GzFFGpF7FGGpGbGxF:GfGfGmG{F9F,GyF+G~FFGuGwFFF4F8F1F#FGwG}";
            //string DNString_dnString = World.pWord(dnString, 2);
            //Console.WriteLine(DNString_dnString);
            return dnString;
        }
        public string Decrypto(string Source)
        {
            try
            {
                byte[] buffer1 = Convert.FromBase64String(Source);
                MemoryStream stream1 = new MemoryStream(buffer1, 0, buffer1.Length);
                string sSource = Source;
                int li_len, i, li_asc;
                string ls_i, ls_code = "";
                int li_ret;
                li_len = sSource.Length;
                ls_code = "";
                li_ret = Convert.ToInt32(sSource.ToCharArray(0, 1)[0]) % 10;
                for (i = 2; i < li_len; i = i + 2)
                {
                    li_asc = Convert.ToInt32(sSource.ToCharArray(i, 1)[0]);
                    if (Convert.ToInt32(sSource.ToCharArray(i - 1, 1)[0]) % 2 == 0)
                    {
                        ls_i = ((char)(li_asc + (i - 1) / 2 + li_ret)).ToString();
                    }
                    else
                    {
                        ls_i = ((char)(li_asc - (i - 1) / 2 - li_ret)).ToString();
                    }
                    ls_code = ls_code + ls_i;
                }
                this.mobjCryptoService.Key = this.GetLegalKey();
                this.mobjCryptoService.IV = this.GetLegalIV();
                ICryptoTransform transform1 = this.mobjCryptoService.CreateDecryptor();
                CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Read);
                StreamReader reader1 = new StreamReader(stream2);
                return reader1.ReadToEnd();
            }
            catch
            {
                return "";
            }
        }
        public static String GetIP()
        {
            //IPHostEntry hostip = Dns.GetHostEntry(Dns.GetHostName());
            //IPAddress[] addrList = hostip.AddressList;
            //return addrList[0].ToString();

            IPHostEntry hostip = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress[] addrList = hostip.AddressList;
            for (int i = 0; i < addrList.Length; i++)
            {
                //toolStripComboBox1.Items.Add(addrList[i].ToString());
                if (addrList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    return addrList[i].ToString();
                    //  strSelectItem = toolStripComboBox1.Items[i].ToString();
                }
            }
            return addrList[0].ToString();
        }

        public string Key;
        private SymmetricAlgorithm mobjCryptoService;

        public SyjiaetricMethod()
        {
            this.mobjCryptoService = new RijndaelManaged();
            //this.Key = Get_key1();
        }

        //public string Decrypto(string Source)
        //{
        //    try
        //    {
        //        byte[] buffer1 = Convert.FromBase64String(Source);
        //        MemoryStream stream1 = new MemoryStream(buffer1, 0, buffer1.Length);

        //        this.mobjCryptoService.Key = this.GetLegalKey();
        //        this.mobjCryptoService.IV = this.GetLegalIV();
        //        ICryptoTransform transform1 = this.mobjCryptoService.CreateDecryptor();
        //        CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Read);
        //        StreamReader reader1 = new StreamReader(stream2);
        //        return reader1.ReadToEnd();
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        public string Encrypto(string Source)
        {
            byte[] buffer1 = Encoding.UTF8.GetBytes(Source);
            MemoryStream stream1 = new MemoryStream();
            this.mobjCryptoService.Key = this.GetLegalKey();
            this.mobjCryptoService.IV = this.GetLegalIV();
            ICryptoTransform transform1 = this.mobjCryptoService.CreateEncryptor();
            CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
            stream2.Write(buffer1, 0, buffer1.Length);
            stream2.FlushFinalBlock();
            stream1.Close();
            byte[] buffer2 = stream1.ToArray();
            return Convert.ToBase64String(buffer2);
        }



        private byte[] GetLegalIV()
        {
            string text1 = Get_key2();
            this.mobjCryptoService.GenerateIV();
            int num1 = this.mobjCryptoService.IV.Length;
            if (text1.Length > num1)
            {
                text1 = text1.Substring(text1.Length - num1, num1);
            }
            else if (text1.Length < num1)
            {
                text1 = text1.PadRight(num1, ' ');
            }
            return Encoding.ASCII.GetBytes(text1);
        }

        private byte[] GetLegalKey()
        {
            string text1 = Get_key1();
            //Console.WriteLine(text1);
            this.mobjCryptoService.GenerateKey();
            int num1 = this.mobjCryptoService.Key.Length;
            if (text1.Length > num1)
            {
                text1 = text1.Substring(text1.Length - num1, num1);
            }
            else if (text1.Length < num1)
            {
                text1 = text1.PadRight(num1, ' ');
            }
            return Encoding.ASCII.GetBytes(text1);
        }


    }
}
